package cat.mpous.bicingmaps;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import cat.pipo.bicing.bean.HttpUtil;
import cat.pipo.bicing.bean.Station;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class BicingMap extends MapActivity {
    
	LinearLayout linearLayout;
	MapView mapView;
	
	List<Overlay> mapOverlays;
	Drawable drawable_green;
	Drawable drawable_orange;
	Drawable drawable_red;
	helloItemizedOverlay itemizedOverlay_green;
	helloItemizedOverlay itemizedOverlay_orange;
	helloItemizedOverlay itemizedOverlay_red;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
                
        // Ens geolocalitza
//        MyLocationOverlay mlo = new MyLocationOverlay(getApplicationContext(), mapView);
//        mlo.enableCompass();
//        mlo.enableMyLocation();
        
        // Fem un zoom per ampliar l'area
        MapController mc = mapView.getController();
        mc.setZoom(5);
                
        mapOverlays = mapView.getOverlays();
        
        drawable_green = this.getResources().getDrawable(R.drawable.green);
        drawable_orange = this.getResources().getDrawable(R.drawable.orange);
        drawable_red = this.getResources().getDrawable(R.drawable.red);
        
        itemizedOverlay_green = new helloItemizedOverlay(drawable_green);
        itemizedOverlay_orange = new helloItemizedOverlay(drawable_orange);
        itemizedOverlay_red = new helloItemizedOverlay(drawable_red);
        
        HttpUtil util = new HttpUtil();
        
        try {
        	Log.d("ESTACIONS", "anem a buscar les estacions del bicing");
        	
        	ArrayList estacionsBicing = util.getAll();

        	int i = 0;
        	for (Iterator iterator = estacionsBicing.iterator(); iterator.hasNext();) {
				Station estacio = (Station) iterator.next();
				Log.d("ESTACIONS", "estacio = "+estacio.getCoords());
				
				int lon = new Double(estacio.getX()*1000000).intValue();
				int lat = new Double(estacio.getY()*1000000).intValue();
				
				GeoPoint punt = new GeoPoint(lat, lon);
				
	            OverlayItem overlayitem = new OverlayItem(punt, estacio.getId().toString(), estacio.getDireccio());
	            
	        	if (estacio.getForatsBuits() < 2) {
	        		itemizedOverlay_red.addOverlay(overlayitem);
	        	}
	        	else if(estacio.getForatsBuits() > 2 && estacio.getForatsBuits() < 10) {
	        		itemizedOverlay_orange.addOverlay(overlayitem);
	        	}
	        	else if(estacio.getForatsBuits() > 10) {
	        		itemizedOverlay_green.addOverlay(overlayitem);
	        	}
	        }

        	mapOverlays.add(itemizedOverlay_green);
        	mapOverlays.add(itemizedOverlay_red);
        	mapOverlays.add(itemizedOverlay_orange);
        	
        	itemizedOverlay_red.popu();
        	itemizedOverlay_green.popu();
        	itemizedOverlay_orange.popu();
        	
        	Log.d("ESTACIONS", "ja tenim les estacions del Bicing!");
        }
        catch(Exception ex) {
        	Log.e("ERROR", "Error:"+ex);
        }
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
	
}